package com.aforce.models;

import javax.media.j3d.*;
import javax.vecmath.*;

public class Spike extends Shape3D {
    public Spike(double x_position, double y_position, double z_position) {
	int i;

	TriangleArray tetra = new TriangleArray(12, TriangleArray.COORDINATES |
		TriangleArray.NORMALS | TriangleArray.TEXTURE_COORDINATE_2);

	
	Point3d p1 = new Point3d(0.0, y_position, z_position);
	Point3d p2 = new Point3d(x_position, 0.0, z_position);
	Point3d p3 = new Point3d(x_position, y_position, 0.0);
	//pointy
	Point3d p4 = new Point3d(x_position * 3, y_position * 3, z_position * 3);
	
	Point3d[] verts = {
		p1, p2, p4,	// front face
		p1, p4, p3,	// left, back face
		p2, p3, p4,	// right, back face
		p1, p3, p2,	// bottom face
	    };
	
	tetra.setCoordinates(0, verts);

	int face;
	Vector3d temp = new Vector3d();
	Vector3f normal = new Vector3f();
	Vector3d v1 = new Vector3d();
	Vector3d v2 = new Vector3d();
	Point3d [] pts = new Point3d[3];
	for (i = 0; i < 3; i++) pts[i] = new Point3d();

	for (face = 0; face < 4; face++) {
	    tetra.getCoordinates(face*3, pts);
	    v1.sub(pts[1], pts[0]);
	    v2.sub(pts[2], pts[0]);
	    temp.cross(v1, v2);
	    
	    normal.set(temp);
	    
	    normal.normalize();
	    for (i = 0; i < 3; i++) {
		tetra.setNormal((face * 3 + i), normal);
	    }
	}
	this.setGeometry(tetra);
	this.setAppearance(new Appearance());
    }
}
